﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMS.DB
{
    class SupplierMng : DBController
    {
        public List<Supplier> GetAllItems()
        {
            var listItems = new List<Supplier>();
            try
            {
                IQueryable<Supplier> query = from item in DataEntities.Suppliers
                                             orderby item.Name
                                             select item;

                listItems = query.ToList();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return listItems;
        }
        public bool IsExist(Supplier input)
        {
            if (input == null)
                return false;
            var itemId = input.ID;
            try
            {
                var output = DataEntities.Suppliers.Where(a => a.ID.ToLower() == itemId.ToLower());
                return output != null && output.Count() > 0 ? true : false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
            }

            return false;
        }
        public bool AddItem(string id, string name, string desc)
        {
            try
            {
                var item = new Supplier { ID = id,
                Name = name,
                Description = desc };
                if (IsExist(item))
                {
                    MessageBox.Show("Nhà cung cấp này đã tồn tại.\nVui lòng kiểm tra lại mã NCC.");
                    return false;
                }
                DataEntities.AddToSuppliers(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
        public Supplier GetItemByElements(string id)
        {
            var Item = new Supplier();
            try
            {
                IQueryable<Supplier> query = from item in DataEntities.Suppliers
                                             where item.ID.Equals(id)
                                             select item;

                Item = query.FirstOrDefault();
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return null;
            }

            return Item;
        }
        public bool DeleteItem(string id)
        {
            try
            {
                var item = new Supplier();
                item = GetItemByElements(id);
                DataEntities.DeleteObject(item);
                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
        public bool UpdateItem(string id, string name, string desc)
        {
            try
            {
                Supplier item;
                item = GetItemByElements(id);
                if (item == null)
                {
                    MessageBox.Show(Constants.Msg.NotItemExist);
                    return false;
                }
                item.Name = name;
                item.Description = desc;

                DataEntities.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                MessageBox.Show(Constants.Msg.ConnectError);
                return false;
            }
        }
    }
}
